//
//  OYHomeOriginalView.swift
//  weiboCopycat
//
//  Created by Orange Yu on 2016/12/5.
//  Copyright © 2016年 Orange Yu. All rights reserved.
//

import UIKit
import SDWebImage

class OYHomeOriginalView: UIView {

    var bottomConstraint:Constraint?
    
     // MARK: -将控件与数据进行连接
    var statusVM:OYStatusViewModel?{
        didSet {
            authorIconView.sd_setImage(with: URL(string:(statusVM?.statusModel?.user?.profile_image_url)!), placeholderImage: UIImage(named: "avatar_default_big"))
            
            authorNameLabel.text = statusVM?.statusModel?.user?.screen_name
            
            authorVerifiedView.image = statusVM?.verifiedImage
            
            authorMbRankView.image = statusVM?.mbRankImage
            
            contentLabel.text = statusVM?.statusModel?.text
            
            sourceLabel.text = statusVM?.contentSource
            
            createTimeLabel.text = statusVM?.composeTime

            // 根据数据中是否有配图传递数据，修改约束
            bottomConstraint?.uninstall()
            if let picUrls = statusVM?.statusModel?.pic_urls ,picUrls.count > 0{
                pictureView.isHidden = false
                self.snp_updateConstraints(closure: { (make) in
                    bottomConstraint = make.bottom.equalTo(pictureView).offset(StatusItemMargin).constraint
                })
                pictureView.picUrls = picUrls

            } else {
                
                pictureView.isHidden = true
                self.snp_updateConstraints(closure: { (make) in
                    bottomConstraint = make.bottom.equalTo(contentLabel).offset(StatusItemMargin).constraint
                })
            }
            
        }
    }
    
     // MARK: -控件的懒加载
    
    /// 微博作者头像控件
    lazy var authorIconView:UIImageView = {
        let authorIconView = UIImageView(image: UIImage(named: "avatar_default_big"))
        authorIconView.layer.cornerRadius = 45 / 2
        authorIconView.layer.masksToBounds = true
        return authorIconView
        
    }()
    /// 用户认证类型控件
    lazy var authorVerifiedView = UIImageView(image: UIImage(named: "avatar_vip"))
    /// 用户等级控件
    lazy var authorMbRankView = UIImageView(image: UIImage(named: "common_icon_membership_level1"))
    /// 微博作者名称控件
    lazy var authorNameLabel: UILabel = {
        let authorNameLabel = UILabel(text: "开智学堂", fontSize: 17, color: UIColor.black)
        return authorNameLabel
    }()
    /// 微博发布时间控件
    lazy var createTimeLabel:UILabel = {
        let timeLabel = UILabel(text: "刚刚", fontSize: 12, color: UIColor.lightGray)
        return timeLabel
    }()
    /// 微博来源控件
    lazy var sourceLabel:UILabel = {
        let sourceLabel = UILabel(text: "来自iPhone6", fontSize: 12, color: UIColor.lightGray)
        return sourceLabel
    }()
    /// 原创微博正文部分
    lazy var contentLabel: UILabel = {
        let contentLabel = UILabel(text: "来自星星的你", fontSize: 16, color:UIColor.black)
        contentLabel.numberOfLines = 0
        return contentLabel
    }()
    /// 图片控件
    lazy var pictureView: OYHomePictureView = OYHomePictureView()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    
    /// 添加子控件并进行布局
    func setupUI() {
        self.backgroundColor = UIColor.white
        
        addSubview(authorIconView)
        addSubview(authorVerifiedView)
        addSubview(authorNameLabel)
        addSubview(createTimeLabel)
        addSubview(sourceLabel)
        addSubview(authorMbRankView)
        addSubview(contentLabel)
        addSubview(pictureView)
        
        authorIconView.snp_makeConstraints { (make) in
            make.top.left.equalTo(self).offset(StatusItemMargin)
            make.size.equalTo(CGSize(width: 45, height: 45))
        }
        
        authorVerifiedView.snp_makeConstraints { (make) in
            make.centerX.equalTo(authorIconView.snp_right).offset(-5)
            make.centerY.equalTo(authorIconView.snp_bottom).offset(-5)
        }
        
        authorNameLabel.snp_makeConstraints { (make) in
            make.left.equalTo(authorIconView.snp_right).offset(StatusItemMargin)
            make.top.equalTo(authorIconView)
            
        }
        
        createTimeLabel.snp_makeConstraints { (make) in
            make.left.equalTo(authorNameLabel)
            make.top.equalTo(authorNameLabel.snp_bottom).offset(5)
        }
        
        sourceLabel.snp_makeConstraints { (make) in
            make.left.equalTo(createTimeLabel.snp_right).offset(5)
            make.top.equalTo(createTimeLabel)
        }
        
        authorMbRankView.snp_makeConstraints { (make) in
            make.left.equalTo(authorNameLabel.snp_right).offset(5)
            make.centerY.equalTo(authorNameLabel)
        }
        contentLabel.snp_makeConstraints { (make) in
            make.top.equalTo(createTimeLabel.snp_bottom).offset(StatusItemMargin)
            make.left.equalTo(self).offset(StatusItemMargin)
            make.right.equalTo(self).offset(-StatusItemMargin)
             // MARK: -删除cell中对originalView高度写死，对最下面的label设置一条约束自动计算view的高度
//            make.bottom.equalTo(self).offset(-StatusItemMargin)
        }
        pictureView.snp_makeConstraints { (make) in
            make.top.equalTo(contentLabel.snp_bottom).offset(StatusItemMargin)
            make.left.equalTo(self).offset(StatusItemMargin)
            
        }
        self.snp_makeConstraints { (make) in
            bottomConstraint = make.bottom.equalTo(contentLabel).offset(StatusItemMargin).constraint
        }

        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
